﻿Imports System.ServiceModel

Public Class frmGetStamp
    Private _random As Random
    Private Sub btnGetStamp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetStamp.Click
        Dim rfc As String = txtRFC.Text
        Dim UUID As String = txtUUID.Text
        Dim transactionID As Int64
        Dim transactionNueva As Int64 = _random.[Next]()
        Dim token As String = String.Empty
        Dim [Error] As String
        Dim Util As New Utilerias()
        [Error] = Util.CreateToken(rfc, transactionID, token)

        If (Not String.IsNullOrEmpty(txtTransactionID.Text)) Then
            Int64.TryParse(txtTransactionID.Text, transactionID)
        End If

        If Not String.IsNullOrEmpty([Error]) Then
            ShowUnknownError([Error])
            Return
        End If
        Try
            Using srvTimbrado = New SrvTimbrado.TimbradoClient()
                Dim respuesta = srvTimbrado.ObtenerTimbrado(rfc, token, transactionNueva, transactionID, UUID)

                txtStamp.Text = respuesta.DatosXML
            End Using
        Catch serviceFault As FaultException(Of SrvTimbrado.FallaServicio)
            Using errorForm = New frmError(TranslateFault.ToServiceError(serviceFault.Detail))
                errorForm.ShowDialog()
            End Using
        Catch sessionFauld As FaultException(Of SrvTimbrado.FallaSesion)
            Using errorForm = New frmError(TranslateFault.ToSessionError(sessionFauld.Detail))
                errorForm.ShowDialog()
            End Using
        Catch ex As Exception
            ShowUnknownError(ex.Message)
        End Try

    End Sub
    Public Sub ShowResult(ByVal message As String)
        MessageBox.Show([String].Format("{0}", message), "Ecodex UniTest")
    End Sub
    Public Sub ShowUnknownError(ByVal message As String)
        MessageBox.Show([String].Format("Error: {0}", message), "Ecodex UniTest")
    End Sub
    Public Sub ShowSessionError(ByVal [error] As Entity.SessionError)
        MessageBox.Show([String].Format("Status {0}: {1}", [error].Status.ToString(), [error].Description), "Ecodex UniTest")
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _random = New Random()
    End Sub
End Class